// ignore_for_file: empty_catches

import 'package:flutter/material.dart';

class AnimationWidget extends StatefulWidget {
  final Widget child;
  final int duration;
  const AnimationWidget({Key? key, required this.child, this.duration = 600})
      : super(key: key);

  @override
  _AnimationWidgetState createState() => _AnimationWidgetState();
}

class _AnimationWidgetState extends State<AnimationWidget>
    with SingleTickerProviderStateMixin {
  //动画控制器
  late AnimationController _controller;
  late Animation<Offset> _animation;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
        duration: Duration(milliseconds: widget.duration), vsync: this);
    _animation = Tween(
      begin: const Offset(0, 0.2),
      end: const Offset(0, 0),
    ).animate(_controller);
    Future.delayed(Duration.zero, () {
      try {
        if (mounted) {
          _controller.forward();
        }
      } catch (err) {}
    });
  }

  @override
  Widget build(BuildContext context) {
    return SlideTransition(
      position: _animation,
      child: widget.child,
    );
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }
}
